<template>
  <div class="app-container">
    <el-input
      class="marginRB10"
      v-model="listQuery.user_id_buyer"
      placeholder="购买人"
      style="width:200px;"
      clearable
    ></el-input>
    <el-input
      class="marginRB10"
      v-model="listQuery.user_id_owner"
      placeholder="拥有人"
      style="width:200px;"
      clearable
    ></el-input>
    <el-input
      class="marginRB10"
      v-model="listQuery.title"
      placeholder="产品名称"
      style="width:200px;"
      clearable
    ></el-input>
    <el-select
      filterable
      v-model="listQuery.category_id"
      class="filter-item marginRB10"
      placeholder="请选择类别"
      style="width: 200px;"
    >
      <el-option
        v-for="item in menuList2"
        :key="item.id"
        :label="item.title"
        :value="item.id"
      ></el-option>
    </el-select>
    <el-date-picker
      class="marginRB10"
      v-model="listQuery.add_time"
      type="date"
      value-format="yyyy-MM-dd"
      placeholder="购买时间"
      style="width: 200px;height: 36px;"
    ></el-date-picker>
    <el-date-picker
      class="marginRB10"
      v-model="listQuery.expired_time"
      type="date"
      value-format="yyyy-MM-dd"
      placeholder="过期时间"
      style="width: 200px;height: 36px;"
    ></el-date-picker>
    <el-input
      class="marginRB10"
      v-model="listQuery.renew_count"
      placeholder="续费次数"
      style="width:200px;"
      clearable
    ></el-input>
    <el-select
      filterable
      v-model="listQuery.state"
      class="filter-item marginRB10"
      placeholder="请选择状态"
      style="width: 200px;"
    >
      <el-option
        v-for="item in selectList1"
        :key="item.key"
        :label="item.lab"
        :value="item.key"
      ></el-option>
    </el-select>
    <el-select
      filterable
      v-model="listQuery.free"
      class="filter-item marginRB10"
      placeholder="是否免费"
      style="width: 200px;"
    >
      <el-option
        v-for="item in selectList2"
        :key="item.key"
        :label="item.lab"
        :value="item.key"
      ></el-option>
    </el-select>
    <el-select
      filterable
      v-model="listQuery.forever"
      class="filter-item marginRB10"
      placeholder="是否永久"
      style="width: 200px;"
    >
      <el-option
        v-for="item in selectList3"
        :key="item.key"
        :label="item.lab"
        :value="item.key"
      ></el-option>
    </el-select>
    <el-input
      class="marginRB10"
      v-model="listQuery.day"
      placeholder="购买天数"
      style="width:200px;"
      clearable
    ></el-input>
    <el-input
      class="marginRB10"
      v-model="listQuery.price"
      placeholder="价格"
      style="width:200px;"
      clearable
    ></el-input>
    <el-button type="primary" @click="searchList" class="marginRB10"
      >搜索</el-button
    >
    <el-button type="primary" @click="resetList" class="marginRB10"
      >重置</el-button
    >

    <el-table
      ref="dataList"
      :data="menuList"
      style="margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="buyer_nickname" label="购买人"></el-table-column>
      <el-table-column prop="owner_nickname" label="拥有人"></el-table-column>
      <el-table-column prop="product_name" label="产品名称"></el-table-column>
      <el-table-column prop="category_name" label="类别"></el-table-column>
      <el-table-column prop="product_picture" label="产品图片">
        <template slot-scope="scope">
          <el-popover placement="right" max-width="220" trigger="hover">
            <img
              :src="scope.row.product_picture"
              alt=""
              style="max-width: 200px;max-height: 200px"
            />
            <img
              slot="reference"
              :src="scope.row.product_picture"
              alt=""
              style="width: 50px;"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="day" label="购买天数"></el-table-column>
      <el-table-column prop="price" label="购买价格"></el-table-column>
      <el-table-column prop="add_time" label="购买日期"></el-table-column>
      <el-table-column prop="expired_time" label="过期时间"></el-table-column>
      <el-table-column prop="renew_count" label="续费次数"></el-table-column>
      <el-table-column prop="state" label="状态">
        <template slot-scope="scope">
          {{ scope.row.state === 0 ? "未使用" : "" }}
          {{ scope.row.state === 1 ? "使用中" : "" }}
          {{ scope.row.state === 2 ? "已过期" : "" }}
        </template>
      </el-table-column>

      <!--      <el-table-column min-width="180" label="操作">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleUpdate(scope.row)" plain>编辑</el-button>-->
      <!--          <cus-del-btn @ok="handleDelete(scope.row)"></cus-del-btn>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>
    <!-- 分页 -->
    <cus-pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.perPage"
      @pagination="getList"
    />

    <el-dialog
      v-dialogDrag
      :title="titleMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      @close="handleDialogClose"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="form"
        label-position="right"
        label-width="120px"
        style="width: 400px; margin: 0 auto;"
      >
        <el-form-item label="名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入名字" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible = false">
          取 消
        </el-button>
        <el-button type="primary" @click="submitForm()">
          确 认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listData, malllistData } from "@/api/mall/mallLog";

export default {
  data() {
    return {
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        perPage: 10,
        user_id_buyer: undefined,
        user_id_owner: undefined,
        title: undefined,
        category_id: undefined,
        add_time: undefined,
        expired_time: undefined,
        renew_count: undefined,
        state: undefined,
        free: undefined,
        forever: undefined,
        day: undefined,
        price: undefined
      },
      menuList: [],
      menuList2: [],
      selectList1: [
        { key: undefined, lab: "全部使用状态" },
        { key: "0", lab: "未使用 " },
        { key: "1", lab: "使用中 " },
        {
          key: "2",
          lab: "已过期"
        }
      ],
      selectList2: [
        { key: undefined, lab: "全部免费状态" },
        { key: "1", lab: "是 " },
        { key: "2", lab: "否" }
      ],
      selectList3: [
        { key: undefined, lab: "全部永久状态" },
        { key: "1", lab: "是 " },
        { key: "2", lab: "否" }
      ],
      dialogFormVisible: false,
      dialogStatus: "",
      titleMap: {
        update: "编辑",
        create: "增加"
      },
      form: {
        id: undefined,
        name: undefined
      },
      rules: {
        name: [{ required: true, message: "请输入名字", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
    this.getList2();
  },
  methods: {
    searchList() {
      this.listQuery.page = 1;
      this.getList();
    },
    getList() {
      this.listLoading = true;
      for (let key in this.listQuery) {
        this.listQuery[key] =
          this.listQuery[key] === "" || this.listQuery[key] === null
            ? undefined
            : this.listQuery[key];
      }
      listData(this.listQuery).then(response => {
        this.menuList = response.data.list;
        this.total = response.data.meta.total;
        this.listLoading = false;
      });
    },
    getList2() {
      malllistData().then(response => {
        this.menuList2 = [
          { id: undefined, title: "全部类别" },
          ...response.data
        ];
      });
    },
    handleCreate(row) {
      this.resetForm();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
    },
    handleUpdate(row) {
      this.form = [];
      this.form.id = row.id;

      this.dialogStatus = "update";
      this.dialogFormVisible = true;

      this.form = Object.assign({}, this.form);
    },

    submitForm() {
      this.$refs["dataForm"].validate(valid => {
        if (valid && this.dialogStatus === "create") {
          let formData = new FormData();
          for (let item in this.form) {
            this.form[item] === undefined
              ? ""
              : formData.append(item, this.form[item]);
          }
          saveData(formData).then(response => {
            if (response.code === 200) {
              this.getList();
              this.$notify({
                type: "success",
                message: "操作成功",
                position: "bottom-right",
                showClose: false,
                duration: 3000
              });
              this.dialogFormVisible = false;
            } else {
              this.$message({
                message: response.message,
                type: "error"
              });
            }
          });
        } else if (valid && this.dialogStatus === "update") {
          let formData = new FormData();
          for (let item in this.form) {
            this.form[item] === undefined
              ? ""
              : formData.append(item, this.form[item]);
          }
          updateData(formData).then(response => {
            if (response.code === 200) {
              this.getList();
              this.$notify({
                type: "success",
                message: "操作成功",
                position: "bottom-right",
                showClose: false,
                duration: 3000
              });
              this.dialogFormVisible = false;
            } else {
              this.$message({
                message: response.message,
                type: "error"
              });
            }
          });
        }
      });
    },
    resetForm() {
      this.form = {
        id: undefined,
        name: undefined
      };
    },
    resetList() {
      this.listQuery = {
        page: 1,
        perPage: 10,
        user_id_buyer: undefined,
        user_id_owner: undefined,
        title: undefined,
        category_id: undefined,
        add_time: undefined,
        expired_time: undefined,
        renew_count: undefined,
        state: undefined,
        free: undefined,
        forever: undefined,
        day: undefined,
        price: undefined
      };
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose() {
      if (this.$refs["dataForm"]) {
        this.$refs["dataForm"].clearValidate(); // 清除整个表单的校验
      }
    }
  }
};
</script>
